package com.example.timetable.method;


import android.content.ContentValues;
import android.content.Context;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;



public class Database extends SQLiteOpenHelper {
        private final static String DB_NAME = "myBase";
         private static String[] TB_NAME= new String[]{"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
        public final static String ID = "_id";
        public final static String CLASS = "classes";
        public final static String LOCA = "location";
        public final static String TEACHER = "teacher";
        public final static String ZHOUSHUS = "zhoushus";//周数起点
        public final static String ZHOUSHUE = "zhoushue";//周数末尾
        public final static String JIESHUS = "jieshus";//节数起点
        public final static String JIESHUE = "jieshue";//节数末尾
        public final static String TIME1 = "time1";//时间起点
        public final static String TIME2 = "time2";//时间末尾
        public final static String WHICH = "which";//单双周信息
        public final static String COLOR = "color";//颜色信息


    public Database(Context context){
        super(context,DB_NAME,null,1);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {

        for (int i = 0; i < 7; i++) {
            String sql = "CREATE TABLE " + TB_NAME[i] + " (_id INTEGER primary key autoincrement,classes varchar(70),location varchar(70)," +
                    "teacher varchar(70),zhoushus varchar(70),zhoushue varchar(70),time1 varchar(70),time2 varchar(70),jieshus varchar(70),jieshue varchar(70),which varchar(70),color varchar(70))";
            db.execSQL(sql);
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oleVersion, int newVersion) {
        for (int i = 0; i < 7; i++) {
            String sql = "DROP TABLE IF EXISTS " + TB_NAME[i];
            db.execSQL(sql);
        }
        onCreate(db);

    }

    public Cursor select(int i) {

        SQLiteDatabase db = Database.this.getReadableDatabase();
        Cursor cursor = db.query(TB_NAME[i], null, null, null, null, null, null);
        return cursor;
    }
    public String Match(String Day){//返回对应的表名
         if(Day == "周一"){ return "Mon";
    }    else if(Day == "周二"){ return "Tue";
    }    else if(Day == "周三"){ return "Wed";
    }    else if(Day =="周四"){ return "Thu";
    }    else if(Day == "周五"){ return "Fri";
    }    else if(Day == "周六"){ return  "Sat";
    }    else if(Day == "周日"){ return  "Sun";
    }
        return " ";//默认返回空
    }

    public long insert(int i, String cla, String loca, String tea, String zhous, String zhoue,String jies,String jiee, String time1, String time2, String which,String color) {
        SQLiteDatabase db = Database.this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(CLASS, cla);
        cv.put(LOCA, loca);
        cv.put(TEACHER, tea);
        cv.put(ZHOUSHUS, zhous);
        cv.put(ZHOUSHUE, zhoue);
        cv.put(JIESHUS, jies);
        cv.put(JIESHUE, jiee);
        cv.put(TIME1,time1);
        cv.put(TIME2, time2);
        cv.put(WHICH, which);
        cv.put(COLOR,color);
        long row = db.insert(TB_NAME[i], null, cv);
        return row;
    }

    public void update(int i, int _id, String cla, String loca, String tea, String zhous,String zhoue,
                       String jies, String jiee,String time1, String time2, String which,String color) {
        SQLiteDatabase db = Database.this.getWritableDatabase();
        String where = "_id = ?";
        String[] whereValues = {Integer.toString(_id)};
        ContentValues cv = new ContentValues();
        if (!cla.equals("")) cv.put(CLASS, cla);
        if (!loca.equals("")) cv.put(LOCA, loca);
        if (!tea.equals("")) cv.put(TEACHER, tea);
        if (!zhous.equals("")) cv.put(ZHOUSHUS, zhous);
        if (!zhous.equals("")) cv.put(ZHOUSHUE, zhoue);
        if (!jies.equals("")) cv.put(JIESHUS, jies);
        if (!jiee.equals("")) cv.put(JIESHUE, jiee);
        if (!time1.equals("")) cv.put(TIME1, time1);
        if (!time2.equals("")) cv.put(TIME2, time2);
        if (!which.equals("")) cv.put(WHICH, which);
        if (!color.equals("")) cv.put(COLOR,color);
        db.update(TB_NAME[i], cv, where, whereValues);
    }

    public void deleteData(int i, int _id) {
        SQLiteDatabase db = Database.this.getWritableDatabase();
        String where = "_id = ?";
        String[] whereValues = {Integer.toString(_id)};
        ContentValues cv = new ContentValues();
        cv.put("classes", "");
        cv.put("location", "");
        cv.put("teacher", "");
        cv.put("zhoushu", "");
        cv.put("jieshu", "");
        cv.put("time1", "");
        cv.put("time2", "");
        cv.put("which", "");
        cv.put("color","");
        db.update(TB_NAME[i], cv, where, whereValues);
    }

    public void delete(int i, int _id) {
        SQLiteDatabase db = this.getWritableDatabase();
        String where = "_id = ?";
        String[] whereValues = {Integer.toString(_id)};
        db.delete(TB_NAME[i], where, whereValues);
    }

    public void createTable(int j) {

        for (int i = 0; i <= 6; i++)
            insert(j, "", "", "", "", "", "", "", "","","","");
    }

}
